package de.baumann.browser.model;

import android.text.TextUtils;


import java.util.List;

import de.baumann.browser.api.net.util.PackHttpUtil;
import de.baumann.browser.api.net.vo.Packet;
import de.baumann.browser.api.net.vo.PacketDetail;
import de.baumann.browser.api.net.vo.PacketMark;
import de.baumann.browser.api.net.vo.PacketRecord;
import de.baumann.browser.api.net.vo.PacketStatus;
import de.baumann.browser.api.net.vo.Result;
import de.baumann.browser.rx.RxSchedulers;
import de.baumann.browser.utils.ErrorMsg;
import io.reactivex.Observable;

public class PackModel extends BaseModel {

    public Observable<Result> sendPack(boolean isUpload, String packDesc, String packImages, String packAmount, String packCount, String packAdUrl, String packLocation) {

        if (TextUtils.isEmpty(packAmount)) {
            return Observable.just(new Result(ErrorMsg.PACK_AMOUNT_EMPTY));
        }

        int amount = Integer.valueOf(packAmount);
        if (amount <= 0) {
            return Observable.just(new Result(ErrorMsg.PACK_AMOUNT_ERROR));
        }

        if (TextUtils.isEmpty(packCount)) {
            return Observable.just(new Result(ErrorMsg.PACK_COUNT_EMPTY));
        }

        int count = Integer.valueOf(packCount);
        if (count <= 0) {
            return Observable.just(new Result(ErrorMsg.PACK_COUNT_ERROR));
        }

        if (count > amount / 2) {
            return Observable.just(new Result(ErrorMsg.PACK_COUNT_ACCOUNT));
        }

        if (TextUtils.isEmpty(packDesc)) {
            return Observable.just(new Result(ErrorMsg.DESC_EMPTY));
        }

        if (TextUtils.isEmpty(packImages)) {
            return Observable.just(new Result(ErrorMsg.IMAGE_URL_EMPTY));
        }

//        if (TextUtils.isEmpty(packAdUrl)) {
//            return Observable.just(new Result(ErrorMsg.PACK_ADURL_EMPTY));
//        }

        if (TextUtils.isEmpty(packLocation)) {
            return Observable.just(new Result(ErrorMsg.PACK_LOCATION_EMPTY));
        }

        if (!isUpload) {
            return Observable.just(new Result("内容检查完毕", 1001));
        }

        return PackHttpUtil.sendPack(packDesc, packImages, amount, count, packAdUrl, packLocation).compose(RxSchedulers.ioMain());
    }


    public Observable<Result<PacketRecord>> getRecivedPacks() {
        return PackHttpUtil.getRecivedPacks().compose(RxSchedulers.ioMain());
    }

    public Observable<Result<PacketRecord>> getSendPacks() {
        return PackHttpUtil.getSendPacks().compose(RxSchedulers.ioMain());
    }

    public Observable<Result<List<PacketMark>>> getPackets(String city) {
        return PackHttpUtil.getPackets( city);
    }

    public Observable<Result<PacketMark>> getPackets1(String city,String token) {
        return PackHttpUtil.getPackets1(getUserId(),  city,token).compose(RxSchedulers.ioMain());
    }

    public Observable<Result<PacketStatus>> getPacketStatus(int packetid) {
        return PackHttpUtil.getPacketStatus( packetid);
    }


    public Observable<Result<PacketStatus>> rePacket(int packetid) {
        return PackHttpUtil.rePacket( packetid).compose(RxSchedulers.ioMain());
    }

    public Observable<Result<PacketDetail>> details(int packetid) {
        return PackHttpUtil.details( packetid).compose(RxSchedulers.ioMain());
    }

}
